package com.zprmb.udzt.activity;


import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zprmb.udzt.BaseActivity;
import com.zprmb.udzt.BbtApplication;
import com.zprmb.udzt.Investcoupon;
import com.zprmb.udzt.LoginActivity;
import com.zprmb.udzt.PayActivity;
import com.zprmb.udzt.R;
import com.zprmb.udzt.V4WebViewRegisterActivity;
import com.zprmb.udzt.V6FingerPayPwdConfirmActivity;
import com.zprmb.udzt.V6LoginActivity;
import com.zprmb.udzt.V6ResultFinsh;
import com.zprmb.udzt.V6WebviewChild;
import com.zprmb.udzt.WebViewActivity;
import com.zprmb.udzt.bean.GetInvestInfo;
import com.zprmb.udzt.bean.JiaoYanBean;
import com.zprmb.udzt.bean.JiaoYanFailBean;
import com.zprmb.udzt.bean.YuePaySuccess;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.CustomDialog;
import com.zprmb.udzt.util.CustomDialog_view;
import com.zprmb.udzt.util.CustomDialog_view2;
import com.zprmb.udzt.util.FingerSetting;
import com.zprmb.udzt.util.FingerprintUtil;
import com.zprmb.udzt.util.GuideAlertDialog;
import com.zprmb.udzt.util.GuideAlertDialog.OnSureClickListener;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.MySharePreferenceUtil;
import com.zprmb.udzt.util.NewVolleryUtil;
import com.zprmb.udzt.util.NumUtil;
import com.zprmb.udzt.util.Regular;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.util.VoteCalculator;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;


public class V6iNewInvestCoupActivity extends BaseActivity implements OnClickListener, OnSureClickListener {
    //    private AddAndSubView addAndSubView;
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_availableAmount, tv_dexterityyue, tv_product_remainamount;
    private TextView tv_min_increaseNum;
    private String name, type, loanid;
    private TextView check_mAgreement;
    private LinearLayout rl_use_coupon;
    private String reward_id;//礼券id
    private DecimalFormat df = new DecimalFormat("###0.00");
    private String red_type = ""; //红包类型
    private int ischange = 0;
    private Double availableGiftAmount = 0.0;//用户红包总量
    private Double coupon_rate = 0.0, red_rate = 0.0;//加息券利率 红包利率
    private TextView coupon_num;//加息券所剩张数
    private TextView coupon_name;//加息券名字
    private int month;
    private Double rate;
    private CheckBox check_dexterity;
    private TextView tv_add_money;
    private boolean use_status = false;
    private String flagFresh;
    private SharedPreferences sha;
    private Double availableAmount = 0.0;
    private TextView tv_product_shouyi;
    int minAmount = 0;
    private boolean finger_Count = false;
    private ImageView img_ck_status;
    private LinearLayout li_tishi;
    private DecimalFormat df2 = new DecimalFormat("###0");
    private Double maxAmount;
    int remainAmount = 0;
    private boolean payPass = true;
    private Double curAmount = 0.0, maxcurAmount = 0.0;
    private boolean nameauth = true, islogin = true;
    protected int increaseNum;
    private CheckBox check;
    private boolean ishuo;
    protected boolean tishi_coupon = true;
    protected boolean resultcoupon = false;
    private TextView tv_tishi;
    private TextView tv_mAgreement;
    private Button btn_invest;
    private boolean flagred;
    private RelativeLayout rl_dexterity;
    private int coupon_max, coupon_min;
    private int useFundAmountInvestFlag = 1;//特权时间
    private String firstType = "";
    private CustomDialog_view2.Builder builder;
    private CustomDialog.Builder mDialog;
    String firstamount = "";
    private GuideAlertDialog mAlertDialog;

    //	private Double proportion;//项目利息availableAmount=63688442.95

    private TextView text_coupon;
    //抵扣的布局
    private LinearLayout layout_dikou;
    //抵扣金额
    private TextView text_dikou;
    //实际金额
    private TextView text_shiji;
    //title
    private int remainAmount2 = 0;
    private TextView text_title;
    //礼券布局
    private LinearLayout layout_liquan;
    int intentpaymoney = 0;
    KeyguardManager keyguardManager = (KeyguardManager) BbtApplication.getContext().getSystemService(BbtApplication.getContext().KEYGUARD_SERVICE);

    //余额全投

    LinearLayout layout_investall;
    TextView text_yue;

    TextView text_quantou;

    //输入的钱数

    EditText edit_zhitoumoney;

    TextView tv_v6_dexterity_pay_in_keyong;

    //清理输入框数据

    ImageView iv_cleardadta;
    LinearLayout layout_clear;
    TextView text_yuji;
    //清除布局
    String money_invest = "";

//红包占位

    View view_zhanwei;
    //提示布局
    LinearLayout layout_tishi;

    LinearLayout li_all;
    static V6iNewInvestCoupActivity ActivityA;
    public static V6iNewInvestCoupActivity instance = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.invest_activity_beifen);
        instance = this;
        setImmersionStatus();
        setContentInview(V6iNewInvestCoupActivity.this).setVisibility(View.INVISIBLE);
        initIntent();
        mAlertDialog = new GuideAlertDialog(this, this);
        tv_title = (TextView) findViewById(R.id.tv_title);
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        text_coupon = (TextView) findViewById(R.id.text_coupon);
        //提示
        layout_tishi = (LinearLayout) findViewById(R.id.layout_tishi);
        //全部布局
        li_all = (LinearLayout) findViewById(R.id.li_all);
        li_all.setOnClickListener(this);
        li_all.performClick();
        initView();
        rl_back.setOnClickListener(this);
        registerBoradcastReceiver();
        layout_dikou = (LinearLayout) findViewById(R.id.layout_dikou);
        //抵扣金额
        text_dikou = (TextView) findViewById(R.id.text_dikou);
        //实际金额
        text_shiji = (TextView) findViewById(R.id.text_shiji);
        layout_dikou.setVisibility(View.GONE);
        //折扣券的产品
        text_title = (TextView) findViewById(R.id.text_title);
        text_title.setVisibility(View.GONE);
        layout_investall = (LinearLayout) findViewById(R.id.layout_investall);
        layout_investall.setOnClickListener(this);
        text_yue = (TextView) findViewById(R.id.text_yue);
        text_quantou = (TextView) findViewById(R.id.text_quantou);
        edit_zhitoumoney = (EditText) findViewById(R.id.edit_zhitoumoney);
        tv_v6_dexterity_pay_in_keyong = (TextView) findViewById(R.id.tv_v6_dexterity_pay_in_keyong);
        iv_cleardadta = (ImageView) findViewById(R.id.iv_cleardadta);
        iv_cleardadta.setOnClickListener(this);
        layout_liquan = (LinearLayout) findViewById(R.id.layout_liquan);
        text_yuji = (TextView) findViewById(R.id.text_yuji);
        layout_clear = (LinearLayout) findViewById(R.id.layout_clear);
        tv_title.setText(name);
        edit_zhitoumoney.requestFocus();
        view_zhanwei = findViewById(R.id.view_zhanwei);
        if (TextUtils.isEmpty(edit_zhitoumoney.getText().toString())) {
//            btn_invest.setBackground(getResources().getDrawable(R.drawable.v6_ud_bg_gray));

            btn_invest.setEnabled(false);
            btn_invest.setBackgroundResource(R.drawable.v6greybutton);
        }

        edit_zhitoumoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (edit_zhitoumoney.getText().length() != 0) {
                    iv_cleardadta.setVisibility(View.VISIBLE);
                    btn_invest.setEnabled(true);
                    btn_invest.setBackgroundResource(R.drawable.v6button);
                    btn_invest.setTextColor(getResources().getColor(R.color.white));
                    int b = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue();
                    if (b <= 0) {
                        text_shiji.setText("0.00");
                    }


                    DecimalFormat df = new DecimalFormat(
                            "######0.00");
                    double a = VoteCalculator
                            .getVoteCalculator_month_Dou(Double.valueOf(edit_zhitoumoney.getText().toString()), rate,
                                    month);
                    text_yuji.setText(df.format(a) + "元");
//                    edit_zhitoumoney


                    int c = Integer.valueOf(dikou);
                    int d = b - c;
                    if (d <= 0) {
                        text_shiji.setText("0");
                    } else {
                        text_shiji.setText(String.valueOf(d));
                    }

                    if (b < coupon_max) {
                        text_coupon.setVisibility(View.GONE);
                        layout_dikou.setVisibility(View.GONE);

                    } else {

                    }

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            try {

                                if (TextUtils.isEmpty(edit_zhitoumoney.getText().toString())) {
                                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                                            "投资金额不能为空");
                                } else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() < minAmount) {
                                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                                            "投资金额不能小于最低起投金额");

                                }
                                if ((Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - minAmount)
                                        % increaseNum != 0) {
                                    tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
//									ToastUtil.showCenterToast(V6investActivity.this,
//											"投资金额的增量要为" + increaseNum + "的倍数");
                                } else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > maxAmount) {
                                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                                            "投资金额不能大于" + maxAmount.intValue() + "!");
                                }
                            } catch (Exception e) {

                            }

                        }
                    }, 1000);


                } else {
                    iv_cleardadta.setVisibility(View.INVISIBLE);
                }
            }
        });
        /**焦点变化监听**/
        edit_zhitoumoney.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (edit_zhitoumoney.getText().length() != 0) {
                    //删除图标显示
                    iv_cleardadta.setVisibility(View.VISIBLE);
                } else {
                    //删除图标隐藏
                    iv_cleardadta.setVisibility(View.INVISIBLE);
                }
                if (arg1) {
                    //得到焦点
                } else {
                    //失去焦点，删除图标隐藏
                    iv_cleardadta.setVisibility(View.INVISIBLE);
                }
            }
        });
        //删除图标的点击监听事件
        iv_cleardadta.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // 执行清空EditText数据
                edit_zhitoumoney.setText("");
                edit_zhitoumoney.setSelection(edit_zhitoumoney.length());
            }
        });
        layout_clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edit_zhitoumoney.setText("");
                edit_zhitoumoney.setSelection(edit_zhitoumoney.length());
            }
        });
        userinfo();
        inintData(1);

        try {
            if (MySharePreferenceUtil.get(V6iNewInvestCoupActivity.this, "chanpin", "").equals("2")) {
                li_all.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showPopupWindow2(li_all);
                    }
                }, 500);
            }
        } catch (Exception e) {

        }


/* if (MySharePreferenceUtil.getBoolean(V6iNewInvestCoupActivity.this, SharePreferenceUtil.FIRST_OPEN1, false) == false) {
            li_all.postDelayed(new Runnable() {
                @Override
                public void run() {
                    showPopupWindow2(li_all);
                }
            }, 500); */

    }
      /*  li_all.postDelayed(new Runnable() {
            @Override
            public void run() {
                showPopupWindow2(li_all);
            }
        }, 500);*/
//		userinfo();


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
//        inintData(0);
    }

    private void myconmment() {
        if (useFundAmountInvestFlag == 0) {

            if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0 && Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > curAmount && islogin && nameauth) {
                if (curAmount == 0) {

                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("特权时间过后即可投资");
                } else {
                    if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0 && Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > maxcurAmount) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("智投宝单笔转投金额不能超过上限" + (df2.format(maxcurAmount / 10000)) + "万!");

                    } else {
                        li_tishi.setVisibility(View.VISIBLE);
                        if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0 && Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > curAmount) {
                            tv_tishi.setText("智投宝余额不足");

                        } else {

                            li_tishi.setVisibility(View.GONE);
                        }
                    }

                }
            } else {
                if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0 && Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > maxcurAmount) {
                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("智投宝单笔转投金额不能超过上限" + (df2.format(maxcurAmount / 10000)) + "万!");

                } else {
                    li_tishi.setVisibility(View.VISIBLE);
                    if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0 && Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > curAmount) {
                        tv_tishi.setText("智投宝余额不足");
                    } else {
                        if (increaseNum != 0) {


                            if (minAmount > remainAmount) {


							/*	if((Integer.valueOf(et_money.getText().toString())!=remainAmount)){
                                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
											"投资金额的增量要为" + increaseNum + "的倍数");
									return;
								}*/
                                li_tishi.setVisibility(View.GONE);
                            } else {
                                if ((Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - minAmount)
                                        % increaseNum != 0) {
                                    li_tishi.setVisibility(View.VISIBLE);
                                    tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
//									ToastUtil.showCenterToast(V6investActivity.this,
//											"投资金额的增量要为" + increaseNum + "的倍数");

                                } else {
                                    li_tishi.setVisibility(View.GONE);
                                }
                            }
                        }
                    }
                }
            }
            if (resultcoupon) {

                if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > coupon_max || Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() < coupon_min) {
                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("投资金额不在福利限投金额内,重新选择福利或重新输入投资金额");
                    li_tishi.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            li_tishi.setVisibility(View.GONE);
                            changeStatus();
                        }
                    }, 2000);
                }
            }
            changeStatus();

        } else {
            if (ishuo) {
                if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0
                        && ishuo && Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > (curAmount + availableAmount)
                        && islogin && nameauth) {

                    if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0 && (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - availableAmount) > maxcurAmount && curAmount >= maxcurAmount) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("智投宝单笔转投金额不能超过上限" + (df2.format(maxcurAmount / 10000)) + "万!");

                    } else {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("账户可用余额不足,请充值!");

//					    	 investComment();
                    }

                } else {
                    if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0 && (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - availableAmount) > maxcurAmount) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("智投宝单笔转投金额不能超过上限" + (df2.format(maxcurAmount / 10000)) + "万!");
                    } else {
                        investComment();
                    }

                }
            } else {
                if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0
                        && availableAmount < Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() && (ishuo == false)
                        && islogin && nameauth) {

                    if (curAmount > 0) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("账户可用余额不足，请充值！");
//							Log.e("提示", "账户可用余额不足，请充值或选择智投宝支付！");

                    } else {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("账户可用余额不足,请充值!");

                    }
//						investComment();
                } else {
                    investComment();


                }
            }
            if (resultcoupon) {
                Log.e("max", coupon_max + "");
                Log.e("min", coupon_min + "");
                if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > coupon_max || Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() < coupon_min) {
                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("投资金额不在福利限投金额内,重新选择福利或重新输入投资金额");
                    li_tishi.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            li_tishi.setVisibility(View.GONE);
                            changeStatus();
                        }
                    }, 2000);
                }
            }
            changeStatus();
        }
    }


    private void investComment() {
        if (increaseNum != 0) {


            if (minAmount > remainAmount) {
//				if((Integer.valueOf(et_money.getText().toString())!=remainAmount)){
//					ToastUtil.showCenterToast(DetailsActivity.this,
//							"投资金额的增量要为" + increaseNum + "的倍数");
//					return;
//				}
                li_tishi.setVisibility(View.GONE);
            } else {
                if ((Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - minAmount)
                        % increaseNum != 0) {
                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
//					ToastUtil.showCenterToast(V6investActivity.this,
//							"投资金额的增量要为" + increaseNum + "的倍数");

                } else {
                    li_tishi.setVisibility(View.GONE);
                }
            }
        }
        if (maxAmount != 0) {
            if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > maxAmount) {
                li_tishi.setVisibility(View.VISIBLE);
//                tv_tishi.setText("最大投资金额为" + maxAmount + "元");
//                tv_tishi.setText("最大投资金额为" + maxAmount + "元");
                tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + " !");
            } else {
                if ((Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - minAmount)
                        % increaseNum != 0) {

                } else {

                    li_tishi.setVisibility(View.GONE);
                }
            }
        }
        if (remainAmount != 0) {
            if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > remainAmount) {

                li_tishi.setVisibility(View.VISIBLE);
                tv_tishi.setText("投资金额不能大于剩余可投金额！");
            } else {
                if ((Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - minAmount)
                        % increaseNum != 0 || Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > maxAmount) {

                } else {

                    li_tishi.setVisibility(View.GONE);
                }
            }
        }
    }

    private void changeStatus() {
        if (li_tishi.getVisibility() == View.VISIBLE) {
            tv_min_increaseNum.setVisibility(View.INVISIBLE);
        } else {
            tv_min_increaseNum.setVisibility(View.VISIBLE);
        }
    }


    private void inintData(final int creat) {
        // TODO Auto-generated method stub
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                setContentInview(V6iNewInvestCoupActivity.this).setVisibility(View.VISIBLE);
                if (jsonObject.optInt("success") == 1) {
//					progressLayout.showContent();
                    LogUtils.e(V6iNewInvestCoupActivity.this, "定期投资页面" + jsonObject.toString());
                    JSONObject json1 = jsonObject.optJSONObject("data");

                    if (json1 != null) {
                        JSONObject user = json1.optJSONObject("user");
                        JSONObject jsoncoupon = json1.optJSONObject("couponInfo");
                        rate = json1.optDouble("rate");
                        minAmount = json1.optInt("minAmount");
                        remainAmount = json1.optInt("remainAmount");
                        remainAmount2 = json1.optInt("minAmount");

                        maxAmount = json1.optDouble("maxAmount");
                        increaseNum = json1.optInt("increaseNum");
                        Log.d("zengliangz", increaseNum + "");
                        edit_zhitoumoney.setHint(remainAmount2 + "元起投" + "," + increaseNum + "元递增");
                        flagFresh = json1.optString("flagFresh");
                        if (user != null) {
                            check_dexterity.setEnabled(true);
                            rl_dexterity.setEnabled(true);
                            islogin = true;
                            d3 = user.optDouble("availableAmount");
                            tv_availableAmount.setText(Regular.getDecimalFormatTwo(user.optDouble("availableAmount")));
                            tv_dexterityyue.setText(Regular.getDecimalFormatTwo(user.optDouble("curAmount")));
//							availableGiftAmount=user.optDouble("availableGiftAmount ");
                            availableAmount = user.optDouble("availableAmount");
                            curAmount = user.optDouble("curAmount");
                            maxcurAmount = user.optDouble("maxTransferInvestAmount");
                            useFundAmountInvestFlag = user.optInt("useFundAmountInvestFlag");
                        } else {
                            check_dexterity.setEnabled(false);
                            rl_dexterity.setEnabled(false);
                            islogin = false;
                            tv_availableAmount.setText("0.00");
                            tv_dexterityyue.setText("0.00");
                            coupon_name.setText("登录查看");
                            coupon_num.setText("");
                        }
                        month = json1.optInt("days");
                      /*  if (creat == 1) {
                            addAndSubView.setNum(json1.optInt("minAmount"));
                        }*/
                        if (jsoncoupon != null) {
                            if (ischange == 2 && !sha.getString("check", "").equals("")) {

                            } else {
                                coupon_name.setTextColor(Color.parseColor("#64A6FE"));
                                coupon_name.setText(jsoncoupon.optString("couponMsg"));
                                if (!coupon_name.getText().toString().equals("登录查看")) {
                                    red_type = "";
                                    resultcoupon = false;
                                    coupon_num.setText(jsoncoupon.optString("couponCount") + "张");
                                }
                            }
                        }

                        tv_product_remainamount.setText(NumUtil.getQiantwo(json1.optString("remainAmount")));

                        tv_min_increaseNum.setText(df2.format(json1.optDouble("minAmount")) + "元起投," + json1.optString("increaseNum") + "元倍数递增");

//						addAndSubView.setMinnum(json1.optInt("minAmount"));
//                        addAndSubView.setincreaseNum(json1.optInt("increaseNum"));
                    }
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
        volleryUtil.setRequestMethod(0);
        volleryUtil.setShow(true);
        volleryUtil.execute(V6iNewInvestCoupActivity.this, Constant.URL + "/api/v6/render/assetDetail/" + type + "/" + loanid, "");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (sha != null) {
                Editor editor = sha.edit();
                editor.clear();
                editor.commit();
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    private void initView() {
        // TODO Auto-generated method stub
     /*   addAndSubView = (AddAndSubView) findViewById(add_subview);
        addAndSubView.setEditTextLayoutWidth(800);
        if (getScreenWidth() > 1080) {
            addAndSubView.setButtonLayoutParm(100, 100);
        } else {
            addAndSubView.setButtonLayoutParm(60, 60);
        }*/
        check = (CheckBox) findViewById(R.id.check);
        check_mAgreement = (TextView) findViewById(R.id.check_mAgreement);
        rl_dexterity = (RelativeLayout) findViewById(R.id.rl_dexterity);
//        addAndSubView.setTextSize(18);
        li_tishi = (LinearLayout) findViewById(R.id.li_tishi);
        tv_mAgreement = (TextView) findViewById(R.id.tv_mAgreement);
        sha = getSharedPreferences("coupon", MODE_PRIVATE);
        img_ck_status = (ImageView) findViewById(R.id.ck_status);
        tv_product_shouyi = (TextView) findViewById(R.id.tv_product_shouyi);
        tv_tishi = (TextView) findViewById(R.id.tv_tishi);
//        addAndSubView.setButtonBgResource(R.drawable.jiahao, R.drawable.jianhao);
        tv_availableAmount = (TextView) findViewById(R.id.tv_availableAmount);
        tv_dexterityyue = (TextView) findViewById(R.id.tv_dexterityyue);
        check_dexterity = (CheckBox) findViewById(R.id.check_dexterity);
        tv_product_remainamount = (TextView) findViewById(R.id.tv_product_remainamount);
        tv_min_increaseNum = (TextView) findViewById(R.id.tv_min_increaseNum);
        rl_use_coupon = (LinearLayout) findViewById(R.id.rl_use_coupon);
        coupon_num = (TextView) findViewById(R.id.coupon_num);
        coupon_name = (TextView) findViewById(R.id.v6_coupon_name);
        tv_add_money = (TextView) findViewById(R.id.tv_add_money);
        btn_invest = (Button) findViewById(R.id.btn_invest);
        check_mAgreement.setOnClickListener(this);
        tv_mAgreement.setOnClickListener(this);
        btn_invest.setOnClickListener(this);
        tv_add_money.setOnClickListener(this);
        rl_use_coupon.setOnClickListener(this);
        rl_dexterity.setOnClickListener(this);
        check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    btn_invest.setEnabled(true);
                    btn_invest.setBackgroundResource(R.drawable.v6button);
                } else {
                    btn_invest.setEnabled(false);
                    btn_invest.setBackgroundResource(R.drawable.btn_unclick);
                }
            }
        });
        check_dexterity.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    img_ck_status.setVisibility(View.VISIBLE);
                    check_dexterity.setChecked(true);
                } else {
                    img_ck_status.setVisibility(View.GONE);
                    check_dexterity.setChecked(false);
                }
                ishuo = isChecked;
//                myconmment();
            }
        });
     /*   addAndSubView.setOnNumChangeListener(new OnNumChangeListener() {

            @Override
            public void onNumChange(View view, int num) {
                Log.e("sha", sha.getString("check", "22"));
                // TODO Auto-generated method stub
                myconmment();
                product_ShouYi();
                if (ischange == 2) {
                    if (use_status) {

                        interest();
                    } else {
                        incoupon();
                    }
                } else {

                }

            }
        });
        tv_title.setText(name);*/
    }

    Double d3 = 0.0;

    private void setImmersionStatus() {
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    private void incoupon() {
        if (!red_type.equals("")) {

            DecimalFormat df = new DecimalFormat(
                    "######0.00");
            double a = VoteCalculator
                    .getVoteCalculator_month_Dou(Double.valueOf(edit_zhitoumoney.getText().toString()), coupon_rate,
                            month);
//			SpannableString titleString = new SpannableString("收益:"+df.format(a)+"元");
//			titleString.setSpan(new ForegroundColorSpan(Color.parseColor("#f0f0f0")), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            coupon_num.setText(Regular.setSpannablecolor("收益:" + df.format(a) + "元", "#cccccc", 0, 3));


        }
    }


    private void initIntent() {
        // TODO Auto-generated method stub
        name = getIntent().getStringExtra("name");
        type = getIntent().getStringExtra("type");
        loanid = getIntent().getStringExtra("id");
    }

    private void userinfo() {
        VolleryUtil vollery = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    JSONObject json1 = jsonObject.optJSONObject("data");
                    Gson gson = new Gson();

                    GetInvestInfo getInvestInfo = new GetInvestInfo();
                    getInvestInfo = gson.fromJson(jsonObject.toString(), GetInvestInfo.class);
//                    tv_product_remainamount.setText(Regular.getDecimalFormatTwo(Double.valueOf(getInvestInfo.getData().getRemainAmount())));
//                    tv_product_remainamount.setText(getInvestInfo.getData().getRemainAmount());
                    /*  if (json1 != null) {
                        JSONObject jsoncoupon = jsonObject.optJSONObject("couponInfo");
                        d3 = json1.optDouble("availableAmount");
                        tv_availableAmount.setText(Regular.getDecimalFormatTwo(json1.optDouble("availableAmount")));
                        tv_dexterityyue.setText(Regular.getDecimalFormatTwo(json1.optDouble("ltbAmount")));
                        tv_product_remainamount.setText(Regular.getDecimalFormatTwo(json1.optDouble("remainAmount")));
                        tv_min_increaseNum.setText(json1.optString("minAmount") + "元起投" + json1.optString("increaseNum") + "元倍数递增");
                        month = json1.optInt("days");
                        if (jsoncoupon != null) {
                            coupon_num.setText(jsoncoupon.optString("couponCount"));
                            coupon_name.setText(jsoncoupon.optString("couponMsg"));
                        }
                    }*/
                }
                LogUtils.e(V6iNewInvestCoupActivity.this, jsonObject.toString());
//				ToJSON.initData2(jsonObject.toString(),"userinfo" );
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        vollery.setRequestMethod(0);
        vollery.setHeaders("token", getToken());

        vollery.execute(this, Constant.URL + "/api/v5/user/userInfo", "");
    }

    private void interest() {
        Log.e("计息", "开始计算");
        if (!red_type.equals("")) {
            if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() != 0) {
                if (red_type.equals("PERCENTAGE")) {
                    double redpage = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() * (0.005);
                    if (redpage > availableGiftAmount) {
                        redpage = availableGiftAmount;
                    }

                } else {
                    Log.e("num", Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() + "----" + month + "---" + Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() * (red_rate / 100) / 365 * month + "   " + red_rate);
                    double redpage = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() * (red_rate / 100) / 365 * month;
                    if (redpage > availableGiftAmount) {
                        redpage = availableGiftAmount;

                    }
                    Log.e("redpage", redpage + "" + availableGiftAmount);
                    coupon_num.setText(Regular.setSpannablecolor("收益:" + df.format(redpage), "#cccccc", 0, 3)
                            + "元");
                    Log.e("shouyi", coupon_num.getText().toString());

                }
            }
        }

    }

    String cardId, getIntentmoney, addtype, myflag = "0";
    String shijie = "0";
    String dikou = "0";
    String touzijine = "0";

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent intent) {
        // TODO Auto-generated method stubx
        super.onActivityResult(arg0, arg1, intent);
        if (intent != null) {
            if (arg1 == 2) {
                addtype = intent.getStringExtra("add_type");
                myflag = intent.getStringExtra("my_flag");
                cardId = intent.getStringExtra("cardId");
                ischange = arg1;
                Log.d(myflag, "myflag3333333333333333");
                layout_liquan.setVisibility(View.GONE);
                view_zhanwei.setVisibility(View.VISIBLE);

                if (myflag.equals("1")) {
                    flagred = true;
                    resultcoupon = false;
                    use_status = true;
                    tishi_coupon = false;
                    layout_dikou.setVisibility(View.GONE);
                    reward_id = intent.getStringExtra("id");
                    //红包
                    red_rate = Double.parseDouble(intent.getStringExtra("rate"));
                    red_type = intent.getStringExtra("red_type");
                    availableGiftAmount = Double.parseDouble(intent.getStringExtra("availableGiftAmount"));
                    Log.e("intent", availableGiftAmount + "");
                    Log.e("hongbaoid", reward_id + "");
                    Log.e("intentrate", intent.getStringExtra("rate") + "");
                    coupon_name.setTextColor(Color.parseColor("#cccccc"));
                    coupon_name.setText(intent.getStringExtra("name"));
                    coupon_name.setVisibility(View.VISIBLE);
                    text_coupon.setText("可用礼券");
                    coupon_num.setVisibility(View.VISIBLE);
                    text_title.setVisibility(View.GONE);
                    layout_dikou.setVisibility(View.GONE);
                    layout_liquan.setVisibility(View.GONE);
                    view_zhanwei.setVisibility(View.VISIBLE);
                    interest();
                    touzijine = intent.getStringExtra("touzijine");
//                    incoupon();
                    text_title.setVisibility(View.GONE);
                } else if (myflag.equals("2")) {
                    //抵扣券

                    layout_dikou.setVisibility(View.VISIBLE);
                    layout_liquan.setVisibility(View.VISIBLE);

                    flagred = false;
                    resultcoupon = true;
                    use_status = false;
                    tishi_coupon = false;
                    getIntentmoney = intent.getStringExtra("getMoney");
                    Log.d("333333333333", getIntentmoney);

                    reward_id = intent.getStringExtra("id");
                    red_type = intent.getStringExtra("red_type");
                    coupon_rate = Double.parseDouble(intent.getStringExtra("rate"));

                    coupon_name.setTextColor(Color.parseColor("#6bacfd"));
                    text_coupon.setText("可用福利");
                    coupon_name.setText(intent.getStringExtra("name"));
                    coupon_name.setVisibility(View.VISIBLE);
                    coupon_max = intent.getIntExtra("maxAmount", 0);
                    coupon_min = intent.getIntExtra("minAmount", 0);
                    Log.e("red_type", red_type + "");
                    Log.e("reward_id", reward_id + "");
//                    text_shiji.setText(String.valueOf(intentpaymoney));
                    coupon_num.setVisibility(View.GONE);
                    text_dikou.setText(intent.getStringExtra("zhekouMoney"));
                    dikou = intent.getStringExtra("zhekouMoney");
                    Log.d("333333333333", dikou);
                    coupon_name.setTextColor(Color.parseColor("#cccccc"));
                    touzijine = intent.getStringExtra("touzijine");
                    view_zhanwei.setVisibility(View.GONE);
                    incoupon();
                    text_title.setVisibility(View.VISIBLE);
                } else {
                    //加息券
                    flagred = false;
                    resultcoupon = true;
                    use_status = false;
                    tishi_coupon = false;
                    layout_dikou.setVisibility(View.VISIBLE);
                    reward_id = intent.getStringExtra("id");
                    red_type = intent.getStringExtra("red_type");
                    text_coupon.setText("可用礼券");
                    layout_liquan.setVisibility(View.GONE);

                    coupon_num.setVisibility(View.VISIBLE);
                    coupon_rate = Double.parseDouble(intent.getStringExtra("rate"));
                    coupon_name.setTextColor(Color.parseColor("#cccccc"));
//                    coupon_name.setVisibility(View.VISIBLE);
                    coupon_name.setText(intent.getStringExtra("name"));
                    Log.d("33333333333333333", intent.getStringExtra("name"));

                    coupon_max = intent.getIntExtra("maxAmount", 0);
                    coupon_min = intent.getIntExtra("minAmount", 0);
                    incoupon();
                    coupon_name.setVisibility(View.VISIBLE);
                    text_title.setVisibility(View.VISIBLE);
                    text_title.setVisibility(View.GONE);
//                    layout_dikou.setVisibility(View.GONE);
                    touzijine = intent.getStringExtra("touzijine");
                    view_zhanwei.setVisibility(View.VISIBLE);
                }
            } else if (arg1 == 1) {
                ischange = arg1;
                flagred = false;
                reward_id = "";
                red_type = "";
                coupon_rate = 0.0;
                use_status = true;
                tishi_coupon = true;
                resultcoupon = false;

                coupon_num.setVisibility(View.VISIBLE);
//                rl_use_coupon.setVisibility(View.GONE);
                layout_liquan.setVisibility(View.GONE);

                layout_liquan.setVisibility(View.INVISIBLE);
                view_zhanwei.setVisibility(View.GONE);
                coupon_name.setVisibility(View.GONE);
                inintData(0);
            }
            if (arg1 == 5) {
                setResult(5, intent);
                finish();
            }

        }
    }

    private void checkUserInfo() {
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                LogUtils.e(V6iNewInvestCoupActivity.this, jsonObject.toString());
                Log.d("33333333333333", "333333333333333");

                JSONObject json = jsonObject.optJSONObject("data");
                if ("false".equals(json.optString("bindCard"))) {
                    mAlertDialog.show();
                    mAlertDialog.setText("请绑定银行卡");
                    mAlertDialog.setTitle("提示");
                    mAlertDialog.setType("bindcard");
                    mAlertDialog.setButtonCancel("稍后");
                    mAlertDialog.setButtonsure("去绑卡");

                    return;
                }
                if ("false".equals(json.optString("payPass"))) {
                    mAlertDialog.show();
                    mAlertDialog.setText("请设置交易密码");
                    mAlertDialog.setTitle("提示");
                    mAlertDialog.setType("password");
                    mAlertDialog.setButtonCancel("稍后");
                    mAlertDialog.setButtonsure("去设置交易密码");
                    return;
                }
                if ("false".equals(json.optString("isRiskAssessment"))) {
                    mAlertDialog.show();
                    mAlertDialog.setText("请进行风险测评");
                    mAlertDialog.setTitle("提示");
                    mAlertDialog.setType("ceping");
                    mAlertDialog.setButtonCancel("稍后");
                    mAlertDialog.setButtonsure("去风险测评");
                    return;
                } else if (getisFinger() == 1) {
                    onFingerprintClick(1);
                } else {
                 /*   if (jsonObject.optString("comment").equals("please login")) {


                        Intent intent = new Intent(V6iNewInvestCoupActivity.this,
                                V6LoginActivity.class);
                        intent.putExtra("close", 1);
                        startActivity(intent);


                    } else {
                        ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                                jsonObject.optString("comment"));
                    }*/
                    onFingerprintClick(1);
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };

    }

    private void verityUser() {
        VolleryUtil vollery = new VolleryUtil() {


            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    Log.e("userinfo", jsonObject.toString());
                    JSONObject json = jsonObject.optJSONObject("data");
//					availableGiftAmount = json.optDouble("availableGiftAmount");
                    payPass = json.optBoolean("payPass");
                    Intent intent = null;


                    if (!json.optBoolean("isBindCard")) {
                        mAlertDialog.show();
                        mAlertDialog.setText("请绑定银行卡");
                        mAlertDialog.setTitle("提示");
                        mAlertDialog.setType("bindcard");
                        mAlertDialog.setButtonCancel("稍后");
                        mAlertDialog.setButtonsure("去绑卡");

                        return;
                    }

                    if ("NOTHING".equals(json.optString("riskKey"))) {
                        mAlertDialog.show();
                        mAlertDialog.setText("请进行风险测评");
                        mAlertDialog.setTitle("提示");
                        mAlertDialog.setType("ceping");
                        mAlertDialog.setButtonCancel("稍后");
                        mAlertDialog.setButtonsure("去风险测评");
                        return;
                    }


                    System.out.println("productdetails=rate===" + rate);
//					if (!json.optBoolean("payPass")) {
//						intent.putExtra("setPass", 1);
//					}


                } else {
                    if (jsonObject.optString("comment").equals("please login")) {


                        Intent intent = new Intent(V6iNewInvestCoupActivity.this,
                                V6LoginActivity.class);
                        intent.putExtra("close", 1);
                        startActivity(intent);


                    } else {
                        ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                                jsonObject.optString("comment"));
                    }
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        vollery.setHeaders("token", getToken());
        vollery.setHeaders("client", "android");
        vollery.setShow(true);
        vollery.setRequestMethod(0);
        vollery.execute(V6iNewInvestCoupActivity.this, Constant.URL
                + "/api/v5/user/userInfo", "");
    }

    /*
    * 实际多少元
    * */


    String flag_zhiwen = "1";

    public void onFingerprintClick(final int type) {
        FingerprintUtil.callFingerPrint(new FingerprintUtil.OnCallBackListenr() {
            @Override
            public void onSupportFailed() {
                showToast("当前设备不支持指纹");
            }

            @Override
            public void onInsecurity() {
                showToast("当前设备未处于安全保护中");
            }

            @Override
            public void onEnrollFailed() {
                showToast("请到设置中设置指纹");
            }

            @Override
            public void onAuthenticationStart() {
//                li_tishi.getVisibility()

                builder = new CustomDialog_view2.Builder(V6iNewInvestCoupActivity.this);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FingerprintUtil.cancel();
                        builder.create().dismiss();
                        builder=null;
                    }
                });
                builder.setMessage("“优点智投”的 Touch ID");
                builder.setMessage2("请验证已有手机指纹");
                builder.setMessage3("测试1");
                builder.setMessage4("测试2");

                builder.create().setCancelable(false);
                builder.create().show();


            }

            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                if (errMsgId == 7) {
                    verityUser();
                } else {

                    showToast(errString.toString());
                }
                if (builder.create() != null) {
                    builder.create().dismiss();
                    builder=null;
                }


            }

            @Override
            public void onAuthenticationFailed() {
                showToast("再试一次");
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                showToast(helpString.toString());
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
//                showToast("解锁成功");
                if (builder.create() != null) {
                    builder.create().dismiss();
                    if (type == 1) {

                        invest();

                    }
                } else {

                }
            }


        });
    }

    /**
     * 指纹投资
     */
    private void invest() {
        AjaxParams params = new AjaxParams();

        params.put("productType", type);
        params.put("productId", loanid);
//        params.put("amount", edit_zhitoumoney.getText().toString());
//        intent.putExtra("money", shijie + "");

        params.put("payType", "FINGERPRINT");
        params.put("useCurrentAmountToInvest", ishuo + "");

        if (myflag.equals("1")) {
            params.put("amount", edit_zhitoumoney.getText().toString() + "");
            params.put("giftId", reward_id);
        }

        else if (myflag.equals("2")) {
            if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() < coupon_max) {
                params.put("amount", edit_zhitoumoney.getText().toString() + "");
            } else {
//                params.put("amount", shijie + "");

                int b = Integer.valueOf(dikou);
                int c = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - b;
                params.put("amount", c + "");

            }

            params.put("cardId", cardId);

        } else if (myflag.equals("3")) {
            params.put("amount", edit_zhitoumoney.getText().toString() + "");
            params.put("raisingratesId", reward_id);
        } else {
            params.put("amount", edit_zhitoumoney.getText().toString() + "");
            params.put("cardId", cardId);
        }

        Log.e("投资", params.toString());


        VolleryUtil vollery = new VolleryUtil() {

            @Override
            public void onUpDate(final JSONObject jsonObject) {
                // TODO Auto-generated method stub
                Log.e("invest", jsonObject.toString());
                if (jsonObject.optInt("success") == 1) {
                    clearCoupon();

                    Log.e("invest", jsonObject.toString());
                   /* ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                            jsonObject.optString("comment"));*/
                    final JSONObject json1 = jsonObject.optJSONObject("data");

                    firstType = json1.optString("firstinvestType");
                    firstamount = json1.optString("ExperienceAmount");
                    if (json1.optBoolean("firstFlag")) {
                        firstType = "1";
                    } else {
                        firstType = "0";
                    }
//
//								if(MainActivity.instance!=null){
//									MainActivity.instance.finish();
//								}
                  /*  Intent intent = new Intent(V6iNewInvestCoupActivity.this, MainActivity.class);
                    intent.putExtra("submit", json1.optString("expectDueDate"));
//                    intent.putExtra("amount", addAndSubView.getNum() + "");
                    intent.putExtra("huo", 1);
                    intent.putExtra("url", "?isInvest=1&flagFresh=" + flagFresh + "&isFirstInvest=" + firstType + "&productType=" + "dq" + "&amount=" + edit_zhitoumoney.getText().toString() + "&action=1");
                    intent.putExtra("firstamount", firstamount);
                    intent.putExtra("firstType", firstType);
                    intent.putExtra("invest", 1);
                    intent.putExtra("useCurrentAmountToInvest", ishuo);
                    intent.putExtra("useCurrAmount", json1.optDouble("useCurrAmount"));
                    intent.putExtra("useFundAmount", json1.optDouble("useFundAmount"));
                    intent.putExtra("rate", json1.optString("count_rate_date"));
//								startActivityForResult(intent,1);
                    Editor editor = getShare().edit();
                    if (firstType.equals("1")) {
                        editor.putInt("firstType", 1);
                        editor.putString("active_url", "?isInvest=1&flagFresh=" + flagFresh + "&isFirstInvest=" + firstType + "&productType=" + "dq" + "&amount=" + edit_zhitoumoney.getText().toString() + "&action=1");
                    } else {
                        editor.putInt("firstType", 0);
                    }
                    editor.commit();
                    setResult(5, intent);
                    finish();*/

                    YuePaySuccess yuePaySuccess = new YuePaySuccess();
                    Gson gson = new Gson();
                    yuePaySuccess = gson.fromJson(jsonObject.toString(), YuePaySuccess.class);


                    Intent intent = new Intent(V6iNewInvestCoupActivity.this, SuccessYueActivity.class);
                    intent.putExtra("active_url", yuePaySuccess.getData().getExpUrl());
                    intent.putExtra("title", tv_title.getText().toString());
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//收益到账
                    Long time = new Long(yuePaySuccess.getData().getExpectDueDate());
                    //起息日期
                    Long time2 = new Long(yuePaySuccess.getData().getCount_rate_date());
                    String d = format.format(time);
                    String e = format.format(time2);
                    intent.putExtra("qixi", e);

                    intent.putExtra("jieshu", d);
                    intent.putExtra("investmoney", yuePaySuccess.getData().getAmount());

                    startActivity(intent);
                    Intent mIntent = new Intent(ACTION_NAME);
                    mIntent.putExtra("yaner", "发送广播，相当于在这里传送数据");
                    //发送广播
                    sendBroadcast(mIntent);
                    Intent mIntent2 = new Intent(ACTION_NAME2);
                    mIntent2.putExtra("yaner", "发送广播，相当于在这里传送数据");
                    //发送广播
                    sendBroadcast(mIntent2);
                    finish();
//							}

                } else {
                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                            jsonObject.optString("comment"));

                    if (jsonObject.optString("comment").equals(
                            "please login")) {
                        if (jsonObject.optString("comment").equals("please login")) {
                            Intent intent = new Intent(V6iNewInvestCoupActivity.this,
                                    LoginActivity.class);
                            intent.putExtra("close", 1);
                            startActivity(intent);
                        }
                    }


                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub
                Intent intent = new Intent(V6iNewInvestCoupActivity.this, V6ResultFinsh.class);
                intent.putExtra("title", "投资结果");

                intent.putExtra("description1", "您好，系统正在处理中，\n请3~5分钟之后前往 “我的账户-定期计划”");

                intent.putExtra("description2", "确认投资结果！若投资失败请重新操作");
                intent.putExtra("button", "我的账户");
                startActivity(intent);


            }
        };


        vollery.setShow(true);
        vollery.setRequestMethod(1);
        vollery.setHeaders("token", getToken());
        vollery.setHeaders("client", "android");
        vollery.setHeaders("deviceId", getdeviceId());
        vollery.setHeaders("deviceToken", getdevice_token());
        vollery.execute(this, Constant.URL
                + "api/v6/invest/invest?", params.toString());


    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e("删除键", "destroy");
        if (sha != null) {
            Editor editor = sha.edit();
            editor.clear();
            editor.commit();
        }
        Log.e("ondestroy", "ondestroy");
    }

    public void flagcoupon() {
        AjaxParams params = new AjaxParams();
        params.put("productId", loanid);
        //金额传实际支付的

//        params.put("amount", addAndSubView.getNum() + "");
        params.put("raisingratesId", reward_id);
        params.put("type", type);
        params.put("useCurrentAmountToInvest", ishuo + "");
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    LogUtils.e("加息券校验", jsonObject.toString());

                    Log.d(jsonObject.toString(), "444444444444");


                } else {

                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
        volleryUtil.setHeaders("deviceId", getdeviceId());
        volleryUtil.setRequestMethod(1);
        volleryUtil.setShow(true);
        volleryUtil.execute(V6iNewInvestCoupActivity.this, Constant.URL + "/api/v6/invest/invests/valid?", params.toString());
    }

    private void showDialog() {
        mDialog = new CustomDialog.Builder(V6iNewInvestCoupActivity.this);

        mDialog.setMessage("优点智投上线指纹支付功能，是否开启使用？");
        mDialog.create().setCancelable(false);
        mDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                mDialog.create().dismiss();
                Editor editor2 = getShare().edit();
                editor2.putInt("invest_num", getInvestNum() + 1);
                editor2.commit();
            }
        });
        mDialog.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                if (!keyguardManager.isKeyguardSecure()) {
                    mDialog.create().dismiss();
                    FingerSetting.openFingerPrintSettingPage(V6iNewInvestCoupActivity.this);

                    return;
                }
                mStartActivity(V6iNewInvestCoupActivity.this, V6FingerPayPwdConfirmActivity.class);
                mDialog.create().dismiss();

            }
        });
        mDialog.create().show();
    }

    String newProduceId;

    String path;
    String myamount;
    String bank_name;
    String investment_Amount;
    String bankName;
    String card_no;
    String type2;
    String loan_id;
    String productId;

    String getBank_name;

    String url;

    /*  private final String ACTION_NAME = "发送广播";
      Intent mIntent = new Intent(ACTION_NAME);
                  mIntent.putExtra("yaner", "发送广播，相当于在这里传送数据");
      //发送广播
      sendBroadcast(mIntent);
      finish();*/
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = null;
        switch (v.getId()) {
            case R.id.rl_back:
                Log.e("删除键", "rl_back");
                if (sha != null) {
                    Editor editor = sha.edit();
                    editor.clear();
                    editor.commit();
                }

                finish();
                break;
            case R.id.rl_use_coupon:
                if (coupon_name.getText().toString().equals("登录查看")) {
                    intent = new Intent(V6iNewInvestCoupActivity.this,
                            V6LoginActivity.class);
                    intent.putExtra("close", 1);
                    startActivity(intent);
                    return;
                } else if (TextUtils.isEmpty(edit_zhitoumoney.getText().toString())) {
                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                            "投资金额不能为空");
                } else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() == 0) {
                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                            "投资金额不能为0");
                }

                /* else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() % 100 != 0) {

                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                            "投资金额为100的整数倍");
                }*/
                else {
                    intent = new Intent(V6iNewInvestCoupActivity.this,
                            Investcoupon.class);
//			intent3.putExtra("rate", rate);
//			intent3.putExtra("month", month);
//			intent3.putExtra("money", et_money.getText().toString());
//			intent3.putExtra("red", availableGiftAmount);
                    intent.putExtra("isnew", false + "");
                    intent.putExtra("isred", false + "");
                    intent.putExtra("id", loanid);
                    intent.putExtra("flag", false + "");
                    intent.putExtra("type", type);

//                intent.putExtra("amount", addAndSubView.getNum());
                    intent.putExtra("amount", Double.valueOf(edit_zhitoumoney.getText().toString()).intValue());


                    startActivityForResult(intent, 0);
                    layout_tishi.setVisibility(View.GONE);
                }

                break;
            case R.id.tv_add_money:
                intent = new Intent(V6iNewInvestCoupActivity.this,
                        V6WebviewChild.class);
                intent.putExtra("url", Constant.URL + "h5/account/recharge/view?app=true&token=");
                intent.putExtra("title", "智投宝");
                startActivity(intent);
                break;
            case R.id.btn_invest:
                checkUserInfo();
                if (coupon_name.getText().toString().equals("登录查看")) {
                    intent = new Intent(V6iNewInvestCoupActivity.this,
                            V6LoginActivity.class);
                    intent.putExtra("close", 1);
                    intent.putExtra("web", 1);
                    startActivityForResult(intent, 1);
                    return;
                }
                if (flagFresh.equals("true")) {
                    if (getisNewUser().equals("false")) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("该项目仅限新手投资");
                        layout_tishi.setVisibility(View.GONE);
                        return;
                    }

                }

//                int m = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue();
                if (TextUtils.isEmpty(edit_zhitoumoney.getText().toString())) {
                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                            "投资金额不能为空");
                    layout_tishi.setVisibility(View.GONE);
                    tishi_coupon = true;
                } else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() < minAmount) {
                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                            "投资金额不能小于最低起投金额");
                    tv_tishi.setText("投资金额不能小于最低起投金额");
                    layout_tishi.setVisibility(View.GONE);
                    tishi_coupon = true;
                    return;
                } else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > maxAmount) {
                    ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                            "投资金额不能大于" + maxAmount.intValue() + "!");
                    tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + "!");
                    layout_tishi.setVisibility(View.GONE);
                    tishi_coupon = true;
                    return;
                } else if (coupon_name.getText().toString().equals("未使用") && tishi_coupon) {
                    tishi_coupon = false;
                    li_tishi.setVisibility(View.VISIBLE);
                    layout_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("亲，您还有未使用的福利哦!");
                    return;
                } else if (getInvestNum() == 0 && getisFingerPay() == 0) {
                    showDialog();

                } else if (getisFingerPay() == 1) {
                    checkUserInfo();
                    //输入金额小于余额
                    tishi_coupon = false;
                    layout_tishi.setVisibility(View.GONE);
                    AjaxParams params = new AjaxParams();
                    params.put("productId", loanid);
                    //金额传实际支付的
                    if (myflag.equals("2")) {
                        int myIntentMoney = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Integer.valueOf(dikou);
                        params.put("amount", myIntentMoney + "");
                    } else {

                        params.put("amount", edit_zhitoumoney.getText().toString());
                    }


                    params.put("type", type);
                    params.put("useCurrentAmountToInvest", ishuo + "");

                    VolleryUtil volleryUtil = new VolleryUtil() {

                        @Override
                        public void onUpDate(JSONObject jsonObject) {
                            // TODO Auto-generated method stub
                            if (jsonObject.optInt("success") == 1) {
                                LogUtils.e("加息券校验", jsonObject.toString());

//                                checkUserInfo();
                                JiaoYanBean jiaoYanBean = new JiaoYanBean();
                                Gson gson = new Gson();
                                jiaoYanBean = gson.fromJson(jsonObject.toString(), JiaoYanBean.class);
                                newProduceId = jiaoYanBean.getData().getProductId();
                                Log.d(jsonObject.toString(), "444444444444");
//                                showPopupWindow(btn_invest);
                                onFingerprintClick(1);
                            } else {
                                JiaoYanFailBean jiaoYanFailBean = new JiaoYanFailBean();
                                Gson gson = new Gson();
                                jiaoYanFailBean = gson.fromJson(jsonObject.toString(), JiaoYanFailBean.class);
                             /*   ToastUtil.showCenterToast(V6iNewInvestCoupActivity.this,
                                        jsonObject.optString("comment"));*/
                                path = jiaoYanFailBean.getData().getPath();
                                myamount = jiaoYanFailBean.getData().getAmount();
                                bank_name = jiaoYanFailBean.getData().getBankName();
                                card_no = jiaoYanFailBean.getData().getCard_no();
                                type2 = jiaoYanFailBean.getData().getType();
                                loan_id = jiaoYanFailBean.getData().getLoan_id();
                                productId = jiaoYanFailBean.getData().getProductId();
                                getBank_name = jiaoYanFailBean.getData().getBank_name();
                                url = jiaoYanFailBean.getSwitchURL();
                                investment_Amount = jiaoYanFailBean.getData().getInvestment_Amount();
                                showPopupWindow(btn_invest);
                            }
                        }

                        @Override
                        public void onError() {
                            // TODO Auto-generated method stub

                        }
                    };
                    volleryUtil.setHeaders("token", getToken());
                    volleryUtil.setHeaders("deviceId", getdeviceId());
                    volleryUtil.setRequestMethod(1);
                    volleryUtil.setShow(true);
                    volleryUtil.execute(V6iNewInvestCoupActivity.this, Constant.URL + "/api/v6/invest/invests/valid?", params.toString());

                } else {
//                    flagcoupon();

                    AjaxParams params = new AjaxParams();
                    params.put("productId", loanid);
                    //金额传实际支付的

//                    int myIntentMoney = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Integer.valueOf(dikou);

                    if (myflag.equals("2")) {
                        int myIntentMoney = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Integer.valueOf(dikou);
                        params.put("amount", myIntentMoney + "");
                    } else {

                        params.put("amount", edit_zhitoumoney.getText().toString());
                    }
//                    params.put("amount", edit_zhitoumoney.getText().toString());
                    params.put("type", type);
                    params.put("useCurrentAmountToInvest", ishuo + "");

                    VolleryUtil volleryUtil = new VolleryUtil() {

                        @Override
                        public void onUpDate(JSONObject jsonObject) {
                            // TODO Auto-generated method stub
                            if (jsonObject.optInt("success") == 1) {
                                LogUtils.e("加息券校验", jsonObject.toString());

                                JiaoYanBean jiaoYanBean = new JiaoYanBean();
                                Gson gson = new Gson();
                                jiaoYanBean = gson.fromJson(jsonObject.toString(), JiaoYanBean.class);
                                newProduceId = jiaoYanBean.getData().getProductId();
                                Log.d(jsonObject.toString(), "444444444444");
                                showPopupWindow(btn_invest);


                            } else {
                                JiaoYanFailBean jiaoYanFailBean = new JiaoYanFailBean();
                                Gson gson = new Gson();
                                jiaoYanFailBean = gson.fromJson(jsonObject.toString(), JiaoYanFailBean.class);

                                path = jiaoYanFailBean.getData().getPath();
                                myamount = jiaoYanFailBean.getData().getAmount();
                                bank_name = jiaoYanFailBean.getData().getBankName();
                                card_no = jiaoYanFailBean.getData().getCard_no();
                                type2 = jiaoYanFailBean.getData().getType();
                                loan_id = jiaoYanFailBean.getData().getLoan_id();
                                productId = jiaoYanFailBean.getData().getProductId();
                                getBank_name = jiaoYanFailBean.getData().getBank_name();
                                url = jiaoYanFailBean.getSwitchURL();
                                investment_Amount = jiaoYanFailBean.getData().getInvestment_Amount();
                                showPopupWindow(btn_invest);
                            }
                        }

                        @Override
                        public void onError() {
                            // TODO Auto-generated method stub

                        }
                    };
                    volleryUtil.setHeaders("token", getToken());
                    volleryUtil.setHeaders("deviceId", getdeviceId());
                    volleryUtil.setRequestMethod(1);
                    volleryUtil.setShow(true);
                    volleryUtil.execute(V6iNewInvestCoupActivity.this, Constant.URL + "/api/v6/invest/invests/valid?", params.toString());

                }





            /*    if (getisFinger() == 1) {
                    checkUserInfo();
                } else {
//                    verityUser();

                    showPopupWindow(btn_invest);
                }*/

                break;
            case R.id.tv_mAgreement:
               /* intent = new Intent(V6iCoupActivity.this,
                        V4WebViewActivity.class);*/
                intent = new Intent(V6iNewInvestCoupActivity.this, V4WebViewRegisterActivity.class);

                intent.putExtra("url", "/landing/mAgreement?app=true&appversion=true&productId=" + loanid);
                intent.putExtra("title", "认购协议");
                intent.putExtra("share", 1);
                startActivity(intent);
                break;
            case R.id.rl_dexterity:
                if (check_dexterity.isChecked()) {
                    img_ck_status.setVisibility(View.GONE);
                    check_dexterity.setChecked(false);
                } else {
                    img_ck_status.setVisibility(View.VISIBLE);
                    check_dexterity.setChecked(true);
                }
                ishuo = check_dexterity.isChecked();
//                myconmment();
                break;
            case R.id.check_mAgreement:
                if (check.isChecked()) {
                    btn_invest.setEnabled(false);
                    btn_invest.setBackgroundResource(R.drawable.btn_unclick);
                    check.setChecked(false);
                } else {
                    btn_invest.setEnabled(true);
                    btn_invest.setBackgroundResource(R.drawable.v6button);
                    check.setChecked(true);
                }
                break;

            case R.id.layout_investall:
            case R.id.text_yue:
            case R.id.text_quantou:
// 账户可用余额               tv_availableAmount
//minAmount 最低起投金额
//increaseNum 投资增量


                /*全投规则：
minAmount=1
                投资金额=(账户可用余额-起投金额)/递增金额 取整后 *递增金额+起投金额
*/
                int a = Double.valueOf(tv_availableAmount.getText().toString()).intValue() - minAmount;
                double b = (double) a / increaseNum;

                Double f = Math.floor(b);
                int c = f.intValue();
//                int c = Double.valueOf(b).intValue();
//                    edit_zhitoumoney.setText(String.valueOf(c));
//                    Toast.makeText(this, a + "", Toast.LENGTH_SHORT).show();

                int d = c * increaseNum + minAmount;
                edit_zhitoumoney.setText(String.valueOf(d));
                edit_zhitoumoney.setSelection(edit_zhitoumoney.length());


                break;

            case R.id.iv_cleardadta:
                edit_zhitoumoney.setText("");
                edit_zhitoumoney.setSelection(edit_zhitoumoney.length());
                break;

        }
    }

    PopupWindow popWindow2;

    private void showPopupWindow2(View parent) {
        if (popWindow2 == null) {
            View view2 = LayoutInflater.from(this)
                    .inflate(R.layout.pop_second2, null);
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow2 = new PopupWindow(view2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop2(view2);
        }
        //设置动画效果
        popWindow2.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow2.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow2.setOutsideTouchable(true);
        popWindow2.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow2.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow2.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop2(View view) {
        final LinearLayout layout_pop_first = (LinearLayout) view.findViewById(R.id.layout_pop_first);

        layout_pop_first.getBackground().setAlpha(175);
        layout_pop_first.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow2.dismiss();
                showPopupWindow3(layout_pop_first);
            }
        });
    }

    PopupWindow popWindow4;

    PopupWindow popWindow3;

    private void showPopupWindow3(View parent) {
        if (popWindow3 == null) {
            View view2 = LayoutInflater.from(this)
                    .inflate(R.layout.pop_third, null);
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow3 = new PopupWindow(view2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop3(view2);
        }
        //设置动画效果
        popWindow3.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow3.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow3.setOutsideTouchable(true);
        popWindow3.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow3.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow3.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop3(View view) {
        final LinearLayout layout_pop_third = (LinearLayout) view.findViewById(R.id.layout_pop_third);

        layout_pop_third.getBackground().setAlpha(175);
        layout_pop_third.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow3.dismiss();
//                showPopupWindow4(layout_pop_third);
                showPopupWindow4(layout_pop_third);
            }
        });

        Button tv_v6_dexterity_pay_in_updata = (Button) view.findViewById(R.id.tv_v6_dexterity_pay_in_updata);
        tv_v6_dexterity_pay_in_updata.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow3.dismiss();
//                showPopupWindow4(layout_pop_third);
                showPopupWindow4(layout_pop_third);
            }
        });
    }


    private void showPopupWindow4(View parent) {
        if (popWindow4 == null) {
            View view2 = LayoutInflater.from(this)
                    .inflate(R.layout.pop_fourth, null);
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow4 = new PopupWindow(view2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop4(view2);
        }
        //设置动画效果
        popWindow4.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow4.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow4.setOutsideTouchable(true);
        popWindow4.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow4.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow4.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop4(View view) {
        LinearLayout layout_pop_third = (LinearLayout) view.findViewById(R.id.layout_pop_fourth);

        layout_pop_third.getBackground().setAlpha(175);
        layout_pop_third.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow4.dismiss();
//                MySharePreferenceUtil.getBoolean(V6iNewInvestCoupActivity.this, SharePreferenceUtil.FIRST_OPEN2, true);

                MySharePreferenceUtil.put(V6iNewInvestCoupActivity.this, "chanpin", "3");
//                MySharePreferenceUtil.clear(V6iNewInvestCoupActivity.this);
            }
        });

        Button button = (Button) view.findViewById(R.id.tv_v6_dexterity_pay_in_updata);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow4.dismiss();
//                MySharePreferenceUtil.getBoolean(V6iNewInvestCoupActivity.this, SharePreferenceUtil.FIRST_OPEN2, true);
                MySharePreferenceUtil.put(V6iNewInvestCoupActivity.this, "chanpin", "3");
//                MySharePreferenceUtil.clear(V6iNewInvestCoupActivity.this);
            }
        });
    }

    private PopupWindow popWindow;

    private void showPopupWindow(View parent) {
        if (popWindow == null) {
            View view = LayoutInflater.from(this)
                    .inflate(R.layout.pop_select_photo2, null);
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    int myamout;

    public String compare(BigDecimal val1, BigDecimal val2) {
        String result = "";
        if (val1.compareTo(val2) < 0) {
            result = "第二位数大！";
        }
        if (val1.compareTo(val2) == 0) {
            result = "两位数一样大！";
        }
        if (val1.compareTo(val2) > 0) {
            result = "第一位数大！";
        }
        return result;
    }

    Double string;

    private final String ACTION_NAME = "发送广播";

    private final String ACTION_NAME2 = "详情页面的跳转";
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_NAME)) {
//                Toast.makeText(V6DetailsActivity.this, "处理action名字相对应的广播", 200);

                finish();
            }
        }

    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTION_NAME);
        //注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }


    public void initPop(View view) {
        ImageView iv_v4_pay_finish = (ImageView) view.findViewById(R.id.iv_v4_pay_finish);
        //点击popwindow消失

        View view_popdismiss = view.findViewById(R.id.pop_top);
        view_popdismiss.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
                popWindow = null;
            }
        });
        //余额
        TextView text_useyue = (TextView) view.findViewById(R.id.text_useyue);

        TextView text_investmoney = (TextView) view.findViewById(R.id.text_investmoney);

        DecimalFormat df = new DecimalFormat("#,###");
        String m = df.format(Double.valueOf(edit_zhitoumoney.getText().toString()).intValue());

        //        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");
//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(m)).toString() + "元");
        text_investmoney.setText(m + ".00" + "元");
        //带银行卡的
        LinearLayout layout_yinhangpay = (LinearLayout) view.findViewById(R.id.layout_yinhangpay);
        TextView text_yinhang = (TextView) view.findViewById(R.id.text_yinhang);
        text_yinhang.setText(myamount + "元");
        //充值投资的按钮
        Button btn_dexterity_invest = (Button) view.findViewById(R.id.btn_dexterity_invest);
        //空白
        View layout_kongbai = view.findViewById(R.id.layout_kongbai);
        LinearLayout layout_yinhang = (LinearLayout) view.findViewById(R.id.layout_yinhang);
        //银行名称
        TextView text_bangname = (TextView) view.findViewById(R.id.text_bangname);
        TextView bank_wei = (TextView) view.findViewById(R.id.bank_wei);

        text_bangname.setText(getBank_name);
        final BigDecimal b1 = new BigDecimal(edit_zhitoumoney.getText().toString());
        final BigDecimal b2 = new BigDecimal(tv_availableAmount.getText().toString());
        //余额小于投资金额
        if (b1.compareTo(b2) > 0) {
            int a = Double.valueOf(tv_availableAmount.getText().toString()).intValue() - Double.valueOf(tv_availableAmount.getText().toString()).intValue() % 100;
            int n = 4;
            bank_wei.setText("(尾号:" + card_no.substring(card_no.length() - 4, card_no.length()) + ")");
            int myIntentMoney = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Integer.valueOf(dikou);
            if (myflag.equals("2")) {
                DecimalFormat df2 = new DecimalFormat("#,###");
                String m1 = df2.format(Double.valueOf(myIntentMoney));

                //        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");
//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(m)).toString() + "元");
                text_useyue.setText(m1 + ".00" + "元");
            } else {
//                text_useyue.setText(Regular.getDecimalFormatTwo(Double.valueOf(tv_availableAmount.getText().toString())) + "元");

                DecimalFormat df2 = new DecimalFormat("#,###");
                String m1 = df2.format(Double.valueOf(tv_availableAmount.getText().toString()).intValue());

                //        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");
//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(m)).toString() + "元");
                text_useyue.setText(m1 + ".00" + "元");
            }
//            text_useyue.setText(Regular.getDecimalFormatTwo(Double.valueOf(tv_availableAmount.getText().toString())) + "元");
            //余额小于于投资金额
            layout_yinhangpay.setVisibility(View.VISIBLE);

            BigDecimal b3 = new BigDecimal(edit_zhitoumoney.getText().toString());
            BigDecimal b4 = new BigDecimal(tv_availableAmount.getText().toString());
            string = b3.subtract(b4).doubleValue();
//            text_yinhang.setText(Regular.getDecimalFormatTwo(Double.valueOf(string)) + "元");
            DecimalFormat df2 = new DecimalFormat("#,###");
            String m1 = df2.format(Double.valueOf(edit_zhitoumoney.getText().toString()).intValue());

            //        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");
//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(m)).toString() + "元");
            text_yinhang.setText(m1 + ".00" + "元");
            layout_yinhang.setVisibility(View.VISIBLE);
            layout_kongbai.setVisibility(View.GONE);
            btn_dexterity_invest.setText("充值并投资");
        } else {

            int myIntentMoney = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Integer.valueOf(dikou);
            if (myflag.equals("2")) {
                DecimalFormat df2 = new DecimalFormat("#,###");
                String m1 = df2.format(Double.valueOf(myIntentMoney).intValue());

                //        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");
//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(m)).toString() + "元");
                text_useyue.setText(m1 + ".00" + "元");
//                text_useyue.setText(Regular.getDecimalFormatTwo(Double.valueOf(myIntentMoney)) + "元");
            } else {
                DecimalFormat df2 = new DecimalFormat("#,###");
                String m1 = df2.format(Double.valueOf(edit_zhitoumoney.getText().toString()).intValue());

                //        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");
//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(m)).toString() + "元");
                text_useyue.setText(m1 + ".00" + "元");
//                text_useyue.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())) + "元");
            }

            btn_dexterity_invest.setText("余额支付");
            layout_yinhang.setVisibility(View.GONE);
            layout_kongbai.setVisibility(View.VISIBLE);
            layout_yinhangpay.setVisibility(View.GONE);
        }
        iv_v4_pay_finish.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
                popWindow = null;
            }
        });
        btn_dexterity_invest.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("3333333333333", getToken());
                String newtoken = getToken();
                try {
                    newtoken = URLEncoder.encode(newtoken, "UTF-8");
//                        myurl1= URLEncoder.encode(myurl1, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                }
                if (b1.compareTo(b2) > 0) {
                    AjaxParams params = new AjaxParams();
                    int myIntentMoney = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Integer.valueOf(dikou);


                    if (myflag.equals("2")) {
//                        intent.putExtra("money", myIntentMoney + "");
                        params.put("amount", myIntentMoney + "");
                    } else {
                        params.put("amount", string + "");
                    }
//                    params.put("amount", string + "");//1
                    params.put("path", path);//1
                    params.put("bankName", bank_name);
                    params.put("card_no", card_no);//1
                    params.put("investment_Amount", investment_Amount);//1
                    params.put("loan_id", loan_id);//1
                    params.put("client", "android");
                    params.put("token", newtoken);//1
                    params.put("type", type2);
//                //1   params.put("bankName", bank_name);         params.put("bank_name", getBank_name);
                    if (myflag.equals("1")) {
//                        params.put("coupon_id", reward_id);
                        params.put("coupon_id", reward_id);
//                        intent.putExtra("red_id", reward_id);cardID
                        //红包
                        params.put("coupon_type", "giftId");
//                        params.put("bankName", "giftId");
//                        params.put("bank_name", bankName);//1

                    } else if (myflag.equals("2")) {
//折扣券
                        params.put("coupon_id", cardId);
//                        params.put("cardID", cardId);
                        params.put("coupon_type", "cardId");
//                        params.put("bank_name", bankName);//1
//                        params.put("bankName",bank_name);
                    } else if (myflag.equals("3"))
//              &coupon_type=&
                    {
                        params.put("coupon_id", "raisingratesId");
//                        params.put("coupon_id", reward_id);
                        params.put("cardID", reward_id);
//                        params.put("bankName",bank_name);
                    } else {
                   /*     params.put("coupon_id", "");
                        params.put("coupon_type", "");*/

//                        params.put("bank_name", bank_name);//1

                    }
                    NewVolleryUtil newVolleryUtil = new NewVolleryUtil() {
                        @Override
                        public void onError() {
                        }

                        @Override
                        public void onUpDate(String jsonObject) {
                            Log.d(jsonObject, "4444444444444444");
                            Intent intent = new Intent(V6iNewInvestCoupActivity.this, NewV6WebviewChild.class);
//                            intent.putExtra("url", url); coupid  还有红包的
//                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            intent.putExtra("url", jsonObject.toString());
                            startActivity(intent);
                            popWindow.dismiss();
                            popWindow = null;
                            finish();
                        }

                    };

                    newVolleryUtil.setShow(true);
                    newVolleryUtil.setRequestMethod(1);

                    newVolleryUtil.setHeaders("deviceId", getdeviceId());



            /*        newVolleryUtil.execute(V6iNewInvestCoupActivity.this,
                            Constant.URL + "hpay/v3/h5recharge?",
                            params.toString());*/
                    newVolleryUtil.execute(V6iNewInvestCoupActivity.this, Constant.URL + "hpay/v3/h5recharge?", params.toString());
                } else {

//                            intent = new Intent(V6iCoupActivity.this, PayActivity2.class);
                    Intent intent = new Intent(V6iNewInvestCoupActivity.this, PayActivity.class);
                    intent.putExtra("flagRedpack", flagred);
                    intent.putExtra("rate", rate);
                    intent.putExtra("qixian", month);
                    intent.putExtra("minAmount", minAmount);

                    intent.putExtra("isZhi", 0);
                    if (payPass) {
                        intent.putExtra("setPass", 0);
                    } else {
                        intent.putExtra("setPass", 1);
                        intent.putExtra("openfingerPay", 1);
                    }
                    intent.putExtra("flagFresh", flagFresh);
                    intent.putExtra("productId", loanid);
                    intent.putExtra("title_tpye", type);
                    intent.putExtra("increaseNum", increaseNum);
                    intent.putExtra("remainAmount", remainAmount);
                    intent.putExtra("myflag", myflag);

                    intent.putExtra("title", tv_title.getText().toString());

//                        getIntentmoney

                    try {
                        int myIntentMoney = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Integer.valueOf(dikou);
                        if (myflag.equals("2")) {
                            intent.putExtra("money", myIntentMoney + "");
                        } else {
                            intent.putExtra("money", edit_zhitoumoney.getText().toString() + "");
                            layout_liquan.setVisibility(View.INVISIBLE);
                            text_title.setVisibility(View.GONE);
                            layout_dikou.setVisibility(View.INVISIBLE);
                        }

                    } catch (Exception e) {

                    }

                    if (myflag.equals("1")) {
                        intent.putExtra("red_id", reward_id);
                    }

                    if (myflag.equals("2")) {
//                        intent.putExtra("money", shijie + "");
//                        intent.putExtra("money", myIntentMoney + "");
                        intent.putExtra("cardId", cardId);
                    } else {
                        intent.putExtra("raisingratesId", reward_id);
                        intent.putExtra("money", edit_zhitoumoney.getText().toString() + "");
                    }


                    intent.putExtra("flagcoupon", resultcoupon);
                    intent.putExtra("useCurrentAmountToInvest", ishuo);
                    startActivityForResult(intent, 0);
                    overridePendingTransition(R.anim.slide_in_from_bottom,
                            R.anim.slide_out_to_bottom);
                }


            }

        });
      /*  //转入按钮
        Button btn_dexterity_invest = (Button) view.findViewById(R.id.btn_dexterity_invest);
        btn_dexterity_invest.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                verityUser();
            }
        });*/
    }


    @Override
    public void onConfirm() {
        // TODO Auto-generated method stub
        Intent intent = null;
        switch (mAlertDialog.getType()) {

            case "bindcard":
                intent = new Intent(V6iNewInvestCoupActivity.this,
                        V6WebviewChild.class);
                intent.putExtra("url", Constant.URL + "h5/account/bankcard/bind/view?app=true&token=");
                intent.putExtra("title", "智投宝");
                startActivity(intent);
                break;
            case "password":
                intent = new Intent(V6iNewInvestCoupActivity.this,
                        V6WebviewChild.class);
                intent.putExtra("url", Constant.URL + "h5/setTraPwd?app=true&token=");
                intent.putExtra("title", "智投宝");
                startActivity(intent);
                break;
            case "ceping":
                Intent intent0 = null;

                intent0 = new Intent(V6iNewInvestCoupActivity.this, WebViewActivity.class);
                intent0.putExtra("url", "h5/assess/answer" + "?app=true&token=");
                // intent0.putExtra("url",
                // "h5/assess/index?showHeader=1"+"?app=true&token=");
                // intent0.putExtra("flag", 1);
                // intent2.putExtra("url",
                // "/h5/messageCenter?app=true&token="+getToken());
                intent0.putExtra("share", 1);
                intent0.putExtra("titleview", 1);
                intent0.putExtra("title", "风险测评");
                startActivity(intent0);
                break;
        }
    }

    @Override
    public void onCancle() {
        // TODO Auto-generated method stub

    }
}
